package com.stars.controller.sys;

import com.github.pagehelper.PageInfo;
import com.stars.common.annotation.UserMenuUpdateRedis;
import com.stars.common.base.BaseCode;
import com.stars.common.base.BaseConstant;
import com.stars.common.base.ResultMsg;
import com.stars.common.result.VueSelectItem;
import com.stars.common.result.VueTreeItem;
import com.stars.common.valid.AddView;
import com.stars.common.valid.UpdateView;
import com.stars.controller.advice.CommonExceptionHandler;
import com.stars.entity.sys.SystemUserRole;
import com.stars.services.system.UserRoleAccessService;
import com.stars.services.system.UserRoleConfigService;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author : dj
 * @version ：1.0.0
 * @Date : create by 2019/1/14
 * @description ：角色组控制层
 * @note 注意事项
 */
@RestController
@RequestMapping(value = "/roleCtl")
@Api(tags = "角色组控制层")
public class UserRoleController extends CommonExceptionHandler {

    @Autowired
    private UserRoleConfigService userRoleConfigServiceImpl;
    @Autowired
    private UserRoleAccessService userRoleAccessServiceImpl;


    /**
     * 获取全部的资源数据
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/roleListTree")
    @ApiOperation(value = "获取全部的资源数据，返回树形")
    public ResultMsg roleListTree() throws Exception {
        List<VueTreeItem> byAll = userRoleAccessServiceImpl.roleTree();
        return ResultMsg.success(byAll);
    }
    @PostMapping("roleAllList")
    @ApiOperation(value = "获取全部的资源数据")
    public ResultMsg roleAllList()throws Exception{
        List<VueSelectItem> byAll = userRoleAccessServiceImpl.findAll();
        return ResultMsg.success(byAll);
    }

    /**
     * 添加角色
     *
     * @param systemUserRole
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/roleInsert")
    @ApiOperation(value = "添加角色")
    @UserMenuUpdateRedis(permission = true)
    public ResultMsg roleInsert(@Validated({AddView.class}) SystemUserRole systemUserRole) throws Exception {
        return userRoleConfigServiceImpl.roleInsert(systemUserRole);
    }

    /**
     * 修改角色
     *
     * @param systemUserRole
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/roleUpdate")
    @ApiOperation(value = "修改角色")
    @UserMenuUpdateRedis(permission = true)
    public ResultMsg roleUpdate(@Validated({UpdateView.class}) SystemUserRole systemUserRole, String menus) throws Exception {
        return userRoleConfigServiceImpl.roleUpdate(systemUserRole,menus);
    }
    @PostMapping(value = "/updatePermission")
    @ApiOperation(value = "根据id,更新菜单")
    @UserMenuUpdateRedis(permission = true)
    public ResultMsg updatePermission(String id,String menus) throws Exception{
      return  userRoleConfigServiceImpl.updatePermission(id,menus);
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param role
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/roleQueryByList")
    @ApiOperation(value = "分页查询")
    public PageInfo<SystemUserRole> roleQueryByList(Integer pageNum, Integer pageSize, SystemUserRole role) throws
            Exception {
        return userRoleAccessServiceImpl.findByPage(pageNum, pageSize, role);
    }


    /**
     * 根据id删除角色
     *
     * @param ids
     * @return
     * @throws Exception
     */
    @PostMapping("/roleDelete")
    @ApiOperation(value = "根据id删除角色")
    @UserMenuUpdateRedis(permission = true)
    public ResultMsg roleDelete(String  ids) throws Exception {
        if(StringUtils.isBlank(ids)){
            return ResultMsg.failure("没有选择删除的数据!");
        }
        for(String id:ids.split(BaseConstant.SPOT)){
            SystemUserRole systemUserRole = userRoleAccessServiceImpl.findByEntityId(id);
            if (systemUserRole == null) {
                return ResultMsg.customMsg(BaseCode.VLIDATE_PARM, "角色不存在!", null);
            }
            //删除内容包括：角色关联的资源关系表，用户关联的角色关系表。
             userRoleConfigServiceImpl.deleteEntity(id);
        }
        return ResultMsg.success();
    }


}
